package com.bookcloud.java21.chapter12.泛型类层次126;

import java.util.ArrayList;
import java.util.List;

public class UserRepository extends GenericRepository<User> {
  private List<User> userList = new ArrayList<>();

  @Override
  public void save(User entity) {
    userList.add(entity);
  }

  @Override
  public User findById(Long id) {
    for (User user : userList) {
      if (user.getId().equals(id)) {
        return user;
      }
    }
    return null;
  }

  @Override
  public void delete(Long id) {
    for (User user : userList) {
      if (user.getId().equals(id)) {
        userList.remove(user);
        break;
      }
    }
  }

  @Override
  public void update(Long id, User entity) {
    for (User user : userList) {
      if (user.getId().equals(id)) {
        user.setName(entity.getName());
        user.setAge(entity.getAge());
      }
    }
  }

  @Override
  public List<User> findAll() {
    // 打印出userList
    for (User user : userList) {
      System.out.println("findAll:" + user);
    }

    return userList;
  }
}
